package com.example.mq.mqserver.datacenter;

import com.example.mq.MqApplication;
import com.example.mq.mqserver.core.Binding;
import com.example.mq.mqserver.core.Exchange;
import com.example.mq.mqserver.core.ExchangeType;
import com.example.mq.mqserver.core.MSGQueue;
import com.example.mq.mqserver.mapper.MetaMapper;

import java.io.File;
import java.util.List;
import java.util.Queue;

// 这个类整合上述的数据库操作
public class DataBaseManager {
    // 从spring容器中获取metaMapper
    private MetaMapper metaMapper;

    // 数据库初始化
    public void init() {
        // 手动获取metaMapper
        metaMapper = MqApplication.context.getBean(MetaMapper.class);

        if(!checkDBExists()) {
            // 先创建一个data目录
            File dataDir = new File("./data");
            dataDir.mkdirs();
            // 不存在则创建
            createTable();
            // 初始化数据
            createDefaultData();
            System.out.println("数据库初始化成功");
        }else {
            System.out.println("数据库已存在");
        }
    }

    public void deleteDB() {
        File file = new File("./data/meta.db");
        boolean ret = file.delete();
        if (ret) {
            System.out.println("数据库文件删除成功");
        }else {
            System.out.println("数据库文件删除失败");
        }

        File dataDir = new File("./data");
        // 使用delete方法删除目录时，目录必须为空，否则删除失败
        ret = dataDir.delete();
        if (ret) {
            System.out.println("data目录删除成功");
        } else {
            System.out.println("data目录删除失败");
        }
    }

    // 创建默认数据，此处主要添加一个默认的交换机
    // RabbitMQ默认创建的交换机类型为direct，且名称为amq.direct
    private void createDefaultData() {
        // 构造一个交换机
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        System.out.println("默认数据创建成功");
    }

    // 创建表，建库并不需要手动执行，首次执行数据库操作的时，会自动创建meta.db
    private void createTable() {
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        System.out.println("表创建成功");
    }

    private boolean checkDBExists() {
        File file = new File("./data/meta.db");
        if (file.exists()) {
            return true;
        }
        return false;
    }

    // 封装其他的数据库操作
    public void insertExchange(Exchange exchange) {
        metaMapper.insertExchange(exchange);
    }

    public List<Exchange> selectAllExchanges() {
        return metaMapper.selectAllExchanges();
    }

    public void deleteExchange(String exchange) {
        metaMapper.deleteExchange(exchange);
    }

    public void insertQueue(MSGQueue queue) {
        metaMapper.insertQueue(queue);
    }

    public List<MSGQueue> selectAllQueues() {
        return metaMapper.selectAllQueues();
    }

    public void deleteQueue(String queue) {
        metaMapper.deleteQueue(queue);
    }
    public void insertBinding(Binding binding) {
        metaMapper.insertBinding(binding);
    }

    public List<Binding> selectAllBindings() {
        return metaMapper.selectAllBindings();
    }

    public void deleteBinding(Binding binding) {
        metaMapper.deleteBinding(binding);
    }


}
